<?php
class Application_Model_Login extends Application_Model_Db_Db
{
    public function getUserRoleByid($userId) {
        $sql = "SELECT
                    `role`.`id`,
                    `role`.`role`,
                    GROUP_CONCAT(`role_department`.`department_id`) AS `department_ids`
                FROM `user_role`
                INNER JOIN `role`
                    ON `user_role`.`role_id` = `role`.`id`
                INNER JOIN `role_department`
                    ON `role`.`id` = `role_department`.`role_id`
                WHERE `user_role`.`user_id` = ?";
        return $this->_db->fetchRow($sql, $userId);
    }

    public function checkIn($userId, $dateLogin) {        
        $where = 'id = ' . $userId; 
        $this->_db->update('user', array('last_visit' => $dateLogin), $where);
    }

    public function getUserBySocialId($socialId) {
        $sql = "SELECT
                    `user`.`access_token`
                FROM `user`
                WHERE `user`.`social_id` = ?";
        return $this->_db->fetchRow($sql, $socialId);
    }

    public function saveSocialUser($data) {
        $this->_db->insert('user', $data);
    }

    public function updateSocialUser($data, $socialId) {
        $this->_db->update('user', $data, 'social_id = ' . intval($socialId));
    }
}